package com.yhsimon.factory.absfactory.pizzastore.order;

import com.yhsimon.factory.absfactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizza {
    //聚合了抽象工厂
    private AbsFactory absFactory;

    public OrderPizza(AbsFactory absFactory){
        setAbsFactory(absFactory);
    }

    private void setAbsFactory(AbsFactory factory){
        Pizza pizza;
        String orderType;
        this.absFactory=factory;
        do {
            orderType=getType();
            //factory 可能是北京的工厂子类，也可能是伦敦的工厂子类
            pizza=factory.createPizza(orderType);
            if(pizza!=null){
                pizza.process();
            }else{
                System.out.println("披萨订购失败");
                break;
            }
        }while (true);
    }

    //获取客户希望订购的披萨种类
    private String getType(){
        try{
            BufferedReader strIn = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type：");
            return strIn.readLine();
        }catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

}
